from PyQt4.QtGui import QWidget,QTableWidget,QVBoxLayout,QMainWindow,QMenu
from PyQt4 import QtCore
from KReport import KStocksItemModel, KStocksTableView
class KReportWidget(QTableWidget):
	ReportSelctionChanged=QtCore.pyqtSignal(str)
	def __init__(self, parent,reportmodel):
		super(KReportWidget, self).__init__(parent)	
		self.mLayout=QVBoxLayout(self)
		self.mLayout.setContentsMargins(0,0,0,0) # set blank space between outside of layout and its parent widget		
		self.mLayout.setSpacing(0)	
		self.reportview=KStocksTableView.KStocksTableView(self)
		self.reportview.ReportSelctionChanged.connect(self.ReportSelctionChanged) # pass up to parent
		self.mLayout.addWidget(self.reportview)
		
		# setup model
		self.reportmodel=reportmodel
		
		self.proxymodel=KStocksItemModel.KStocksSortItemModel(parent) # set the proxy
		self.proxymodel.setSourceModel(self.reportmodel)
		self.reportview.setModel(self.proxymodel)
		
		# forward methods to model
		for m in [ 'UpdateSymbolList', 'UpdateDynamicColumns','GetCurrentSymbolList']: 
			setattr(self, m, getattr(self.reportmodel, m))
		

		
	def OnSymbolChanged(self,symbol):
		# triggered when selected symbol changed, informed by parent
		itemlist=self.reportmodel.findItems(QtCore.QString(symbol),QtCore.Qt.MatchExactly,0)
		if len(itemlist)>0:
			item=itemlist[0]
			# select row should be in terms of proxcy model(since source model index would be different with the view after sorting)
			acindex=self.proxymodel.mapFromSource(item.index()) 
			self.reportview.selectRow(acindex.row())
			self.reportview.scrollTo(acindex,self.PositionAtCenter)
